<?php

namespace Api\Controller;


use Think\Controller;

class MyController extends Controller
{
    /**
     * 推广二维码
     */
    public function qrcode()
    {
        $url = I('get.url');
        $customer_id = $this->get_customer_id();
        Vendor('phpqrcode.phpqrcode');
        //生成二维码图片
        $object = new \QRcode();
        $url = $url . "?uid=" . $customer_id;//网址或者是文本内容
//        $url = "http://jewelry.bndvip.com/index.php?m=Api&c=WeChat&a=start&uid=" . $customer_id;//网址或者是文本内容
        $level = 3;
        $size = 4;
        $errorCorrectionLevel = intval($level);//容错级别
        $matrixPointSize = intval($size);//生成图片大小
        $object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);
    }

    /**
     * 获取个人信息
     */
    public function My()
    {
        $openid = $this->get_openid();

        if ($openid) {
            $result = M('user')->where("openid='$openid'")->field(array('nickname', 'id', 'headimgurl', 'sign', 'new_img', 'new_name'))->find();
            if (!empty($result['new_img'])) {
                $result['headimgurl'] = $result['new_img'];
            }
            if (!empty($result['new_name'])) {
                $result['nickname'] = $result['new_name'];
            }
            if(empty($result['sign'])){
                $result['sign']='';
            }
            $this->ajaxreturn(array('state' => 1, 'data' => $result), 'json');
        } else {
            $this->ajaxreturn(array('state' => 0), 'json');
        }
    }

    /**
     * 关于我们
     */
    public function aboutMy()
    {
        $result = M('about')->order('id desc')->find();
        $result['content'] = htmlspecialchars_decode($result['content']);
        if ($result) {
            $this->ajaxreturn(array('state' => 1, 'data' => $result), 'json');
        } else {
            $this->ajaxreturn(array('state' => 0), 'json');
        }
    }

    /**
     * 修改密码
     */
    public function editPassword()
    {
        $data['old_password'] = md5(I('request.old_password'));
        $openid = $this->get_openid();
        $data['new_password'] = md5(I('request.password'));
        if ($openid) {
            $password = M('user')->where("openid='$openid'")->getField('password');
            if ($password == $data['old_password']) {
                $result = M('user')->where("openid='$openid'")->setField('password', $data['new_password']);
                if ($result === false) {
                    //修改失败
                    $this->ajaxreturn(array('state' => -2), 'json');
                } else {
                    //修改成功
                    $this->ajaxreturn(array('state' => 1), 'json');
                }
            } else {
                //原密码不对
                $this->ajaxreturn(array('state' => -1), 'json');
            }
        } else {
            //没登录
            $this->ajaxreturn(array('state' => 0), 'json');
        }
    }

    /**
     * 重置密码
     */
    public function resetPassword()
    {
        $mobile = I('request.mobile');
        $password = I('request.password');
        $type = I('request.type');
        if ($type == 1) {
            $result = M('user')->where("mobile='$mobile'")->setField('password', $password);
            if ($result === false) {
                //失败
                $this->ajaxreturn(array('state' => -2), 'json');
            } else {
                //成功
                $this->ajaxreturn(array('state' => 1), 'json');
            }
        } else {
            //验证不通过
            $this->ajaxreturn(array('state' => -1), 'json');
        }
    }

    /**
     * 更改头像
     */
    public function saveheadimg()
    {
        $openid = $this->get_openid();

        if ($openid) {
            $url = I('request.file');
            $result = M('user')->where("openid='$openid'")->setField('new_img', $url);
            if ($result === false) {
                $this->ajaxreturn(array('state' => 0), 'json');
            } else {
                $this->ajaxreturn(array('state' => 1), 'json');
            }
//            if ($_POST['submit']) {
//                if ($_FILES['image']['size']) {
//                    $file_uploads = file_get_contents($_FILES['image']['tmp_name']);
//                    $file_uploads = base64_encode($file_uploads);
//                    $result = M('user')->where("id='$user_id'")->setField('headimgurl', $file_uploads);
//                    if ($result) {
//                        $this->ajaxreturn(array('state' => 1), 'json');
//                    } else {
//                        //图片保存失败
//                        $this->ajaxreturn(array('state' => -2), 'json');
//                    }
//
//                } else {
//                    //图片提交失败
//                    $this->ajaxreturn(array('state' => -3), 'json');
//                }
//            } else {
//                //没有提交图片
//                $this->ajaxreturn(array('state' => -4), 'json');
//            }
        } else {
            //没登录
            $this->ajaxreturn(array('state' => -1), 'json');
        }
    }

    /**
     * 更改信息
     */
    public function saveUsername()
    {
        $data['sign'] = I('request.sign');
        $data['new_name'] = I('request.user_name');
        $openid = $this->get_openid();
        if ($openid) {

                $user_result=M('user')->where("openid='$openid'")->find();
                if(empty($data['sign'])){
                    $sign=$user_result['sign'];
                    $data['sign']=$sign;
                }
                if(empty($data['new_name'])){
                    $user_name=$user_result['nickname'];
                    $data['new_name']=$user_name;
                }
                $result = M('user')->where("openid='$openid'")->save($data);

                if ($result === false) {
                    //失败
                    $this->ajaxreturn(array('state' => -1), 'json');
                } else {
                    //成功
                    $this->ajaxreturn(array('state' => 1), 'json');
                }

        } else {
            //没登录
            $this->ajaxreturn(array('state' => 0), 'json');
        }
    }

    /**
     * 更新数据库的个人信息
     */
    public function updateUser()
    {
        $openid = I('request.openId');
        if(empty($openid)){
            $this->ajaxreturn(array('state'=>-1),'json');
        }
        $data['nickname'] = I('request.nickname');
        $data['headimgurl'] = I('request.headimgurl');
        $data['sex'] = I('request.sex');
        $result = M('user')->where("openid='$openid'")->save($data);
        if ($result === false) {
            $this->ajaxreturn(array('state' => 0), 'json');
        } else {
            $this->ajaxreturn(array('state' => 1), 'json');
        }
    }


}